package sg.edu.nus.comp.cs4218.impl.fileutils;

import java.io.File;

import sg.edu.nus.comp.cs4218.impl.ATool;
import sg.edu.nus.comp.cs4218.fileutils.IPwdTool;

public class PWDTool extends ATool implements IPwdTool {
	/**
	 * Constructor
	 *
	 */
	public PWDTool() {
		super((String [])null);
		setStatusCode(0);
	}

	/**
	 * Retrieves the absolute path of the current working directory
	 * 
	 * @param directory
	 *           - The current working directory
	 * @return A string representation of the absolute path
	 */
	@Override
	public String getStringForDirectory(File directory) {
		// Error Handling
		if (directory == null || !directory.exists()
				|| !directory.isDirectory()) {
			setStatusCode(1);
			return "Error: Cannot find working directory";
		}
		// Processing the command
		return directory.getAbsolutePath();
	}

	/**
	 * Retrieves the absolute path of the current working directory
	 * 
	 * @param stdin
	 *           - Input on stdin. NOT THE ARGUMENTS! Can be null.
	 * @return Output on stdout
	 */
	@Override
	public String execute(File workingDir, String stdin) {
		return getStringForDirectory(workingDir);
	}
}
